const Order = require("../model/order.model");

class OrderService {
    async createOrder(order) {
        try {
            return await Order.create(order);
        } catch (error) {
            console.log(error);
            return;
        }
    }

    async findAllOrder(pageNum, pageSize, state) {
        try {
            const { count, rows } = await Order.findAndCountAll({
                attributes: ['goods_info', 'total', 'order_number', 'state'],
                where: {
                    state,
                },
                offset: (pageNum - 1) * pageSize,
                limit: pageSize * 1,
            });

            return {
                pageNum,
                pageSize,
                total: count,
                list: rows,
            };
        } catch (error) {
            return console.log(error);
        }
    }

    async updateOrder(id, state){
        return await Order.update({state}, {where:{id}})
    }
}

module.exports = new OrderService();
